{{ header }}{{ column_left }}
<div id="content">
	<div class="page-header">
		<div class="container-fluid">
			<div class="float-end">
				<button type="button" data-bs-toggle="tooltip" title="{{ button_filter }}" onclick="$('#filter-customer').toggleClass('d-none');" class="btn btn-light d-md-none d-lg-none"><i class="fas fa-filter"></i></button>
				<button type="button" id="button-approve" value="{{ approve }}" data-bs-toggle="tooltip" title="{{ text_approve }}" class="btn btn-success"><i class="fas fa-check"></i></button>
				<button type="button" id="button-deny" value="{{ deny }}" data-bs-toggle="tooltip" title="{{ text_deny }}" class="btn btn-warning"><i class="fas fa-times"></i></button>
			</div>
			<h1>{{ heading_title }}</h1>
			<ol class="breadcrumb">
				{% for breadcrumb in breadcrumbs %}
					<li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
				{% endfor %}
			</ol>
		</div>
	</div>
	<div class="container-fluid">
		<div class="row">
			<div id="filter-customer" class="col-lg-3 col-md-12 order-lg-last d-none d-lg-block mb-3">
				<div class="card">
					<div class="card-header"><i class="fas fa-filter"></i> {{ text_filter }}</div>
					<div class="card-body">
						<div class="mb-3">
							<label class="form-label">{{ entry_customer }}</label> <input type="text" name="filter_customer" value="" placeholder="{{ entry_customer }}" id="input-customer" class="form-control"/>
						</div>
						<div class="mb-3">
							<label class="form-label">{{ entry_email }}</label> <input type="text" name="filter_email" value="" placeholder="{{ entry_email }}" id="input-email" class="form-control"/>
						</div>
						<div class="mb-3">
							<label for="input-customer-group" class="form-label">{{ entry_customer_group }}</label> <select name="filter_customer_group_id" id="input-customer-group" class="form-select">
								<option value=""></option>
								{% for customer_group in customer_groups %}
									<option value="{{ customer_group.customer_group_id }}">{{ customer_group.name }}</option>
								{% endfor %}
							</select>
						</div>
						<div class="mb-3">
							<label for="input-type" class="form-label">{{ entry_type }}</label> <select name="filter_type" id="input-type" class="form-select">
								<option value=""></option>
								<option value="customer">{{ text_customer }}</option>
								<option value="affiliate">{{ text_affiliate }}</option>
							</select>
						</div>
						<div class="mb-3">
							<label for="input-date-added" class="form-label">{{ entry_date_added }}</label>
							<div class="input-group date">
								<input type="text" name="filter_date_added" value="" placeholder="{{ entry_date_added }}" id="input-date-added" class="form-control"/>
								<div class="input-group-text"><i class="fas fa-calendar"></i></div>
							</div>
						</div>
						<div class="text-end">
							<button type="button" id="button-filter" class="btn btn-light"><i class="fas fa-filter"></i> {{ button_filter }}</button>
						</div>
					</div>
				</div>
			</div>
			<div class="col-lg-9 col-md-12">
				<div class="card">
					<div class="card-header"><i class="fas fa-list"></i> {{ text_list }}</div>
					<div class="card-body">
						<div id="customer-approval">{{ list }}</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript"><!--
$('#customer-approval').on('click', '.pagination a', function(e) {
    e.preventDefault();

    $('#customer-approval').load(this.href);
});

$('#customer-approval').load('index.php?route=customer/customer_approval|list&user_token={{ user_token }}');

$('#button-approve, #button-deny, #button-delete').on('click', function(e) {
    e.preventDefault();

    var element = this;

    $.ajax({
        url: $(element).val(),
        type: 'post',
        dataType: 'json',
        data: $('#customer-approval input[name=\'selected[]\']:checked'),
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            if (json['error']) {
                $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                $('#customer-approval').load('index.php?route=customer/customer_approval|list&user_token={{ user_token }}');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#customer-approval').on('click', '.btn-success, .btn-danger', function(e) {
    e.preventDefault();

    var element = this;

    $.ajax({
        url: $(element).attr('href'),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            if (json['error']) {
                $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                $('#customer-approval').load('index.php?route=customer/customer_approval|list&user_token={{ user_token }}');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#button-filter').on('click', function() {
    var url = '';

    var filter_customer = $('#input-customer').val();

    if (filter_customer) {
        url += '&filter_customer=' + encodeURIComponent(filter_customer);
    }

    var filter_email = $('#input-email').val();

    if (filter_email) {
        url += '&filter_email=' + encodeURIComponent(filter_email);
    }

    var filter_customer_group_id = $('#input-customer-group').val();

    if (filter_customer_group_id !== '') {
        url += '&filter_customer_group_id=' + encodeURIComponent(filter_customer_group_id);
    }

    var filter_type = $('#input-type').val();

    if (filter_type !== '') {
        url += '&filter_type=' + filter_type;
    }

    var filter_date_added = $('#input-date-added').val();

    if (filter_date_added) {
        url += '&filter_date_added=' + encodeURIComponent(filter_date_added);
    }

    $('#customer-approval').load('index.php?route=customer/customer_approval|list&user_token={{ user_token }}' + url);
});

$('#input-customer').autocomplete({
    'source': function(request, response) {
        $.ajax({
            url: 'index.php?route=customer/customer|autocomplete&user_token={{ user_token }}&filter_name=' + encodeURIComponent(request),
            dataType: 'json',
            success: function(json) {
                response($.map(json, function(item) {
                    return {
                        label: item['name'],
                        value: item['customer_id']
                    }
                }));
            }
        });
    },
    'select': function(item) {
        $('#input-customer').val(item['label']);
    }
});

$('#input-email').autocomplete({
    'source': function(request, response) {
        $.ajax({
            url: 'index.php?route=customer/customer|autocomplete&user_token={{ user_token }}&filter_email=' + encodeURIComponent(request),
            dataType: 'json',
            success: function(json) {
                response($.map(json, function(item) {
                    return {
                        label: item['email'],
                        value: item['customer_id']
                    }
                }));
            }
        });
    },
    'select': function(item) {
        $('#input-email').val(item['label']);
    }
});

$('.date').datetimepicker({
    'format': 'YYYY-MM-DD',
    'locale': '{{ datepicker }}',
    'allowInputToggle': true
});
//--></script>
{{ footer }}
